package com.woniuxy.mq02.configuration;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.PostConstruct;

/**
 * @Author author
 * @Create 2024-03-01 16:32
 */
//@Component
public class RabbitProducerCallbackListner implements RabbitTemplate.ConfirmCallback, RabbitTemplate.ReturnCallback {

    @Autowired
    RabbitTemplate rabbitTemplate;

    //SpringBean的生命周期<bean id="" class="" init-method="" destroy-method="">
    //init-method对象初始化的时候执行， @PostConstruct注解就是init-method属性
    @PostConstruct
    public void init(){
        //管理rabbitTemplate类和RabbitProducerCallbackListner类关联
        rabbitTemplate.setConfirmCallback(this);
        rabbitTemplate.setReturnCallback(this);
    }


    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        System.out.println("confirm监控消息是否进入了交换机");
        System.out.println("管理数据，可以自定义，默认没有分配correlationData:"+correlationData);
        System.out.println("True消息投递到了交换机，false没有进入交换机，ack:"+ack);
        System.out.println("cause:"+cause);
    }

    @Override
    public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) {
        System.out.println("return监控,消息如果不能通过交换机投递到队列，触发return,类似退信操作");
        System.out.println("message:"+message);
        System.out.println("replyCode:"+replyCode);
        System.out.println("replyText:"+replyText);
        System.out.println("exchange:"+exchange);
        System.out.println("routingKey:"+routingKey);
    }
}
